/****************************************************************************
*created by yang.zheng on 2020-08-17 for login page
****************************************************************************/

<template>
    <div class="login-container">
        <div class="loginbox">
            <div class="blank"></div>
            <span class="title">综合信息管理系统  登录</span>
            <div class="userTextBox">
                <span class="el-icon-s-custom">
                </span>
                <input v-model="userId" type="text" class="userText" placeholder="用户名">
            </div>
            <div class="userPwdBox">
                <span class="el-icon-lock"></span>
                <input v-model="userPwd" type="password" class="userPwd" placeholder="密码">
            </div>
            <el-checkbox class="remember" label="" v-model="isreb" >记住密码</el-checkbox>
            <span @click="regEvt" class="regsiter">注册新用户</span>
            <div>
                <span class="loginBtn" @click="loginEvt">登录</span>
            </div>
        </div>
    </div>
</template>

<script>

import ajax from '@/util/Ajax'

import cryptoJS from 'crypto-js'

export default {
    data() { 
        return {
            userId : '',
            userPwd : '',
            isreb : false,
            key : 'yang.zheng'
        } 
    },
    methods: {
        async loginEvt () {
            let obj = {
              userId : this.userId,
              userPwd : cryptoJS.MD5(this.userPwd).toString().toLocaleUpperCase()
            };
            console.log(obj)
            let res = await ajax({
                url : 'user/login',
                data : obj,
                method : 'post'
            })
            if (res.code === 200) {
                if (this.isreb) {
                    let str = cryptoJS.AES.encrypt(JSON.stringify({userId : this.userId, userPwd : this.userPwd}),this.key).toString()
                    localStorage.setItem('userInfo',str)
                }else {
                    localStorage.removeItem('userInfo')
                }

                this.$message.success('登录成功')
                // sessionStorage.setItem('user_info', JSON.stringify(res.data));
                this.$store.dispatch('common/setUserInfo',res.data);
                this.$router.push('/home')
            }else {
              this.$message.error(res.message)
            }
        },

        regEvt () {
            this.$router.push('/register')
        }
    },
    
    created () {
        let cache = localStorage.getItem('userInfo')
        if (cache) {
            try {
                this.isreb = true;
                let infoText = JSON.parse(cryptoJS.AES.decrypt(cache,this.key).toString(cryptoJS.enc.Utf8));
                this.userId = infoText.userId;
                this.userPwd = infoText.userPwd
            }catch (e) {
                console.log(e)
                this.isreb = false;
                localStorage.removeItem('userInfo')
            }
            
        }
    }   
}
</script>

<style lang="less" scoped>
    @import "@/styles/common";
    @import "@/styles/login";
    
</style>